package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.Payment;

import com.example.chatchatbackend.entity.vo.request.PaymentVO;
import com.example.chatchatbackend.mapper.PaymentMapper;
import com.example.chatchatbackend.service.PaymentService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {
    
    private static final LocalDateTime currentTime = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private PaymentMapper paymentMapper;
    @Override
    public List<Payment> getAllPayment() {
        return paymentMapper.getAllPayment();
    }

    @Override
    public PaymentVO insertPayment(PaymentVO paymentVO) {
        Payment payment = new Payment(null,paymentVO.getProductId(),paymentVO.getPaymentStatus(),this.currentTime,this.currentTime,paymentVO.getCustomerId(),paymentVO.getTrackNumber());
        if(this.save(payment))
            return paymentVO;
        return null;
    }

    @Override
    public List<PaymentVO> insertPaymentList(List<PaymentVO> paymentVOList) {

        List<Payment> paymentList = new ArrayList<>();

        for (PaymentVO paymentVO : paymentVOList) {
            paymentList.add(new Payment(null,paymentVO.getProductId(),paymentVO.getPaymentStatus(),this.currentTime,this.currentTime,paymentVO.getCustomerId(),paymentVO.getTrackNumber()));
        }
        paymentMapper.insertList(paymentList);
        return paymentVOList;
    }
    

    @Override
    public IPage<Payment> pageThree(Integer pageNum, Integer pageSize) {
        Page<Payment> page = new Page<>(pageNum, pageSize);
        IPage<Payment> paymentIPage = paymentMapper.selectAll(page, null);
        return paymentIPage;
    }

    @Override
    public PaymentVO updatePayment(PaymentVO paymentVO) {
        Payment payment = new Payment();
        BeanUtils.copyProperties(paymentVO, payment);
        payment.setUpdateTime(this.currentTime);
        paymentMapper.updatePayment(payment);
        return paymentVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        paymentMapper.deleteBatchIds(ids);
    }
}
